package com.example.exercise.demo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.demo.vo.AppFocusVO;
import com.example.exercise.demo.dao.entity.AppFocusEntity;
import com.example.exercise.demo.dao.AppFocusDAO;

/**
* 
*/
@Service
public class AppFocusService {

    @Autowired
    private AppFocusDAO appFocusDAO;

    public List<AppFocusVO> findByWhere(Page<?> page, AppFocusVO appFocusVO){
        List<AppFocusVO> list = new ArrayList<>();
        List<AppFocusEntity> list0 = appFocusDAO.findByWhere(page, appFocusVO);
        for (AppFocusEntity appFocusEntity : list0) {
            list.add(toAppFocusVO(appFocusEntity));
        }
        return list;
    }

    public AppFocusVO findByKey(AppFocusVO appFocusVO){
        AppFocusEntity appFocusEntity = appFocusDAO.findByKey(appFocusVO);
        return toAppFocusVO(appFocusEntity);
    }

    public int insert(AppFocusVO appFocusVO){
        return appFocusDAO.insert(toAppFocusEntity(appFocusVO));
    }

    public int updateByKey(AppFocusVO appFocusVO){
        return appFocusDAO.updateByKey(toAppFocusEntity(appFocusVO));
    }

    public int deleteByKey(AppFocusVO appFocusVO){
        return appFocusDAO.deleteByKey(appFocusVO);
    }

    private AppFocusVO toAppFocusVO(AppFocusEntity appFocusEntity) {
        AppFocusVO appFocusVO = new AppFocusVO();
                appFocusVO.setId(String.valueOf(appFocusEntity.getId()));
        appFocusVO.setUserId(String.valueOf(appFocusEntity.getUserId()));
        appFocusVO.setUserName(appFocusEntity.getUserName());
        appFocusVO.setFansId(String.valueOf(appFocusEntity.getFansId()));
        appFocusVO.setFansName(appFocusEntity.getFansName());

        return appFocusVO;
    }

    private AppFocusEntity toAppFocusEntity(AppFocusVO appFocusVO) {
        AppFocusEntity appFocusEntity = new AppFocusEntity();
                String id = appFocusVO.getId();
        if(id != null) {
            appFocusEntity.setId(Integer.parseInt(appFocusVO.getId()));
        }
        String userId = appFocusVO.getUserId();
        if(userId != null) {
            appFocusEntity.setUserId(Integer.parseInt(appFocusVO.getUserId()));
        }
        appFocusEntity.setUserName(appFocusVO.getUserName());
        String fansId = appFocusVO.getFansId();
        if(fansId != null) {
            appFocusEntity.setFansId(Integer.parseInt(appFocusVO.getFansId()));
        }
        appFocusEntity.setFansName(appFocusVO.getFansName());

        return appFocusEntity;
    }

}